{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/remote-home/yushengliao/yushengliao/Medical_Consultation_Evaluation/src/data_tools\n",
      "/remote-home/yushengliao/yushengliao/Medical_Consultation_Evaluation/src\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "# 获取当前Notebook文件所在的目录\n",
    "notebook_dir = os.path.abspath(os.path.dirname(\"__file__\"))\n",
    "print(notebook_dir)\n",
    "# 获取src文件夹的父目录，并设置为工作目录\n",
    "project_root = os.path.dirname(notebook_dir)\n",
    "print(project_root)\n",
    "os.chdir(project_root)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils.general_utils import npy2json\n",
    "from utils.metric import acc\n",
    "from utils.general_utils import hisotry2str\n",
    "# from utils import metric, general_utils"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.8266666666666667"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_datas = npy2json(\"/Users/liaoyusheng/Documents/liao/Projects/Medical Consultation Evaluation/Medical_Consultation_Evaluation/results/MedQA/data_test/GPT4测评_medqa_UB.npy\")\n",
    "# datas = list(raw_datas.values())\n",
    "datas = []\n",
    "select_idx = []\n",
    "for key, value in raw_datas.items():\n",
    "    value[\"id\"] = key\n",
    "    if \"diagnosis\" in value[\"question\"]:\n",
    "        select_idx.append(key)\n",
    "        datas.append(value)\n",
    "\n",
    "print(len(datas))\n",
    "acc(datas, answer_key=[\"raw_data\", \"answer_idx\"], output_key=\"final_answer\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.47333333333333333"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "raw_datas = npy2json(\"/Users/liaoyusheng/Documents/liao/Projects/Medical Consultation Evaluation/Medical_Consultation_Evaluation/results/MedQA/data_test/GPT4测评_LB.npy\")\n",
    "# datas = list(raw_datas.values())\n",
    "datas = []\n",
    "for key, value in raw_datas.items():\n",
    "    if key in select_idx:\n",
    "        # select_idx.append(value)\n",
    "        datas.append(value)\n",
    "\n",
    "print(len(datas))\n",
    "acc(datas, answer_key=[\"raw_data\", \"answer_idx\"], output_key=\"final_answer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "with open(\"/Users/liaoyusheng/Documents/liao/Projects/Medical Consultation Evaluation/Medical_Consultation_Evaluation/datasets/MedQA/preprocess_datas/medqa_processed.json\", \"r\") as f:\n",
    "    datas = json.load(f)\n",
    "\n",
    "diagnosis_datas = []\n",
    "\n",
    "for data in datas:\n",
    "    if \"diagnosis\" in data[\"question\"]:\n",
    "        diagnosis_datas.append(data)\n",
    "\n",
    "diagnosis_datas.sort(key=lambda x:x[\"id\"])\n",
    "\n",
    "with open(f\"/Users/liaoyusheng/Documents/liao/Projects/Medical Consultation Evaluation/Medical_Consultation_Evaluation/datasets/MedQA/preprocess_datas/medqa_diagnosis_select.json\", \"w\", encoding=\"utf-8\") as f:\n",
    "    json.dump(diagnosis_datas, f, indent=4, ensure_ascii=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "def b(data, a, b, c):\n",
    "    print(a)\n",
    "    print(b)\n",
    "    print(c)\n",
    "\n",
    "def a(data, *kwargs):\n",
    "    b(data, *kwargs)\n",
    "\n",
    "a(1, 2, 3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "pattern = re.compile(r'\\([A-Z]\\)')\n",
    "# 查找所有匹配项\n",
    "s = \"(A) Bicarbonate (B) Gastric acid (C) Glucagon (D) Insulin (E) Intrinsic factor\"\n",
    "n = len(pattern.findall(s))\n",
    "print(n)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "med_llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
